<?php
/**
 * Created by 绵阳人维网络科技有限公司.
 * User: 罗东华
 * Date: 2016/3/17
 * Time: 10:54
 */
namespace Admin\Logic;
class MenuLogic extends AdminLogicBase{

    /*后台菜单*/
    public function getMenu(){

            if(S('Admin_menu'.session('ldh_userid'))==false){
            if(\Admin\Controller\AdminBase::$role_ids){
                $where['id']=array('in',\Admin\Controller\AdminBase::$role_ids);
            }
            $menus=$this->order('listorder ASC,id ASC')->where($where)->select();
            foreach($menus as $k=>$v){
                 $data[$v['parentid']][]=$v;
            }
            $menu_tree=$this->menu_tree($data[0],$data);
            $Admin_menu['menu_tree']=$menu_tree;
            $Admin_menu['menu_p']=$data[0];
            S('Admin_menu'.session('ldh_userid'),$Admin_menu);
            return $Admin_menu;
        }else{ 
            return S('Admin_menu'.session('ldh_userid'));
        }

    }
    /*递归菜单*/
    public function menu_tree($menu,&$data){

        foreach($menu as $k=> $v){
            if($data[$v['id']]){
                $menu[$k]['child']=$this->menu_tree($data[$v['id']],$data);
            }

        }
        return $menu;

    }
    /*递归菜单字符串 0是 html菜单 1是 多选菜单*/
    public function menu_str($tree,$s='',$a='',$type=0){
             $str='';$count=count($tree);$i=0;

             if($type==1){ $a.='&nbsp;│';}else{  $a.='&nbsp;&nbsp;&nbsp;│';}
            foreach($tree as $k=>$v){
                $i++;
                if($count==$i){
                    if($type==1){ $b='&nbsp;└─  ';}else{  $b='&nbsp;&nbsp;&nbsp;└─  ';}

                }
                $x=$b?$b:$s;
                if($v['parentid']==0){$x='';$a='';}

            if($v['status']==1){$stu='显示';}else{$stu='隐藏';}
                if($type==1){

                    $str.="<option value='".$v['id']."'>".substr($a,0,strlen($a)-9).$x.$v['name']."</option>";
                }else{
                    $str.="  <tr>
          <td align='center'><input name='listorders[".$v['id']."]' type='text' size='3' value='".$v['listorder']."' class='input-text radius btn'></td>
          <td align='center'>".$v['id']."</td>
          <td>".substr($a,0,strlen($a)-21).$x.$v['name']."</td>
          <td align='center'>".$stu." </td>
          <td align='center'>
          <a href='".U('Admin/menu_add',array('pid'=>$v['id']))."'>添加子菜单</a> |
          <a href='".U('Admin/menu_save',array('id'=>$v['id']))."'>修改</a> |
          <a  href='javascript:;' onclick='menu_del(this,".$v['id'].")' >删除</a>
          </td>
        </tr>";
                }

                 if(is_array($v['child'])){


                     if($type==1){ $b='&nbsp;├─  ';}else{  $b='&nbsp;&nbsp;&nbsp;├─  ';}

                    $str.= $this->menu_str($v['child'],$b,$a,$type);
                }

            }
             return $str;

    }

    /*后天菜单字符串  * */
    public function getMenu_str($type=0){

      $getMenu=$this->getMenu();
      $menu_tree=$getMenu['menu_tree'];
       $Menu_str=$this->menu_str($menu_tree,'','',$type);
        return $Menu_str;


    }


}